window.ga = window.ga
  || function() {
    (ga.q = ga.q || []).push(arguments);
  };
ga.l = +new Date();
// 跳转更新页面
document
  .getElementsByClassName('js-to-update')[0]
  .addEventListener('click', function() {
    let bg = chrome.extension.getBackgroundPage();
    bg.bgMethods.updateTab();
  });

const showUpdate = res => {
  let cdnVersion = res.version;
  let cdnVersionNum = cdnVersion.replace(/\./g, '');
  let manifest = chrome.runtime.getManifest();
  let localVersion = manifest.version.replace(/\./g, '');
  let domUpdate = document.getElementById('update');
  if (cdnVersionNum > localVersion) {
    domUpdate.style.display = 'inline-block';
    document.getElementsByClassName('cdn-version')[0].innerHTML = cdnVersion;
  } else {
    domUpdate.style.display = 'none';
  }
};

document.addEventListener('DOMContentLoaded', function() {
  let bg = chrome.extension.getBackgroundPage();
  bg.bgMethods
    .getCurrentVersion()
    .then(res => {
      try {
        res = JSON.parse(res);
        showUpdate(res);
      } catch (e) {
        console.log(e);
      }
    })
    .catch(e => {
      console.log(e);
    });
  let baseInfo = bg.baseInfo || {};
  let manifest = chrome.runtime.getManifest();
  document.getElementById('versionNumber').innerHTML = manifest.version;
  ga('create', {
    trackingId: 'UA-121977257-1',
    cookieDomain: 'auto',
    alwaysSendReferrer: true,
    userId: (baseInfo.companyInfo && baseInfo.companyInfo.accountBookId) || ''
  });
  ga('set', 'checkProtocolTask', function() {}); // Removes failing protocol check. @see: http://stackoverflow.com/a/22152353/1958200
  ga('set', 'referrer', baseInfo.referer);
  ga('send', 'pageview', '/popup.html');
});


// 零申报零时按钮
document.getElementById('tableDetail').onclick = () => {
  console.log(1);
  chrome.runtime.sendMessage(
    {
      event: 'asyncTableDetailStart',
      data: {}
    },
    function(response) {
      console.log(response);
    }
  );
};
// 成功返回
chrome.runtime.onMessage.addListener(function(
  request,
  sender,
  sendResponse
) {
  if (request.event === 'zeroReportBack') {
    console.log(request);
  }
});
